
<%
	/*<!-- Imports -->*/
%>
<%@page import="java.util.LinkedList"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.Hashtable"%>
<%@page import="canss.supports.basic"%>
<%@page import="canss.supports.Makequery_select"%>
<%
	/*<!-- Component start -->*/
%>
<%
	{
%>
<%
	/*<!-- Initiation -->*/
%>
<%
	response.setContentType("text/html; charset=UTF-8");
		canss.web.controls.WebDBControl webdbcontrol = null;
		webdbcontrol = (webdbcontrol == null) ? new canss.web.controls.WebDBControlBean() : webdbcontrol;
%>
<%
	/*<!-- Preparing inputs -->*/
%>
<%
	int i = 0;
		String _query = basic.IIf((String) request.getParameter("_query"), "");
%>
<%
	/*<!-- Query Compilation -->*/
%>
<%
	if (_query.length() == 0)
			return;

		if (_query.matches("(?i)^select.*"))
		{//select
			Iterator<String[]> iter = null;
			try
			{
				LinkedList<String[]> res = webdbcontrol.Select(_query);
				//calc nums
				String[] num_wrapchar = null;
				Iterator<String[]> iter2 = res.iterator();
				while (iter2.hasNext())
				{
					String[] curhashtmp = iter2.next();
					if (num_wrapchar == null)
					{
						num_wrapchar = new String[curhashtmp.length];
						for (i = 0; i < num_wrapchar.length; num_wrapchar[i++] = "")
							;
						continue; //pass the col_name
					}
					for (i = 0; i < curhashtmp.length; i++)
					{
						try
						{
							Double.parseDouble(curhashtmp[i]);//check whether a number or a string
						} catch (NumberFormatException e)
						{
							num_wrapchar[i] = "\"";
						}
					}
				}
				//---------
				iter = res.iterator();

				boolean isfirstiter = true;
				String[] colnames = null;

				out.write("[");
				while (iter.hasNext())
				{
					String[] curhashtmp = iter.next();

					if (isfirstiter)
					{
						isfirstiter = false;
						colnames = curhashtmp;
						continue;
					}
					out.write("{");

					for (i = 0; i < curhashtmp.length; i++)
						out.write(String.format("\"%s\":%s%s%s%s", colnames[i], num_wrapchar[i], curhashtmp[i], num_wrapchar[i], (i + 1 < curhashtmp.length ? "," : "")));

					out.write((iter.hasNext() ? "}," : "}"));
				}
				out.write("]");
			} catch (java.io.IOException e)
			{
				e.printStackTrace();
			}
		} else if (_query.matches("(?i)^update.*") || _query.matches("(?i)^insert.*"))
		{//insert/update
			int[] retlist = webdbcontrol.InsertNUpdate(_query);
			out.write("[");
			if (retlist != null)
			{
				for (i = 0; i < retlist.length; i++)
				{
					out.write(String.format("{\"ret\":%d}%s", retlist[i], (i + 1 < retlist.length ? "," : "")));
				}
			}
			out.write("]");
		}
%>

<%
	}
%>
<%
	/*<!-- Component end -->*/
%>